#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
const int maxn = 5 + 2e6;
int rad() {
    int tmp;
    scanf("%d", &tmp);
    return tmp;
}

int n;
set<int> s[6];

int asiz;
struct Node {
    int num, time, id, op; // 几个人，时间，第几波，人类型
    bool operator<(const Node &x) const { return time < x.time; }
} a[maxn];
int sit[maxn]; // id to 桌子
int cnt[maxn]; // 桌子 i 有几个空位

int main() {
    n = rad();
    for (int i = 1; i <= n; ++i) {
        int x = rad(), s = rad(), t = rad(), op = rad();
        a[++asiz] = {x, s, i, op};  // insert
        a[++asiz] = {x, t, -i, op}; // erase
    }
    sort(a + 1, a + 1 + asiz);
    for (int i = 1; i <= n; ++i) s[4].insert(i), cnt[i] = 4;
    int ans = 0;
    for (int i = 1; i <= asiz; ++i) {
        if (a[i].id > 0) {
            int from, to, tmp = n + 1; // 当前桌子的编号
            if (a[i].op == 0) {
                from = 4, to = 4 - a[i].num;
                tmp = *(s[from].begin());
            } else {
                for (int j = a[i].num; j <= 4; ++j) {
                    if (s[j].empty()) continue;
                    int now = *(s[j].begin());
                    if (now < tmp) tmp = now, from = j, to = j - a[i].num;
                }
            }
            ans = max(ans, tmp);
            s[to].insert(tmp), s[from].erase(tmp);
            sit[a[i].id] = tmp; // 这波人坐的桌子
            cnt[tmp] = to;
        } else {
            int zuozi = sit[-a[i].id];
            s[cnt[zuozi]].erase(zuozi);
            s[cnt[zuozi] + a[i].num].insert(zuozi);
            cnt[zuozi] += a[i].num;
        }
    }
    cout << ans;
}